package photostock.control.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import photostock.model.Album;
import photostock.model.ItemFactory;
import photostock.model.User;
import photostock.view.WidgetFactory;

public class AjaxServlet extends HttpServlet {

	private final static long serialVersionUID = 20090501L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		if (null != request.getParameter("action")) {
			if (request.getParameter("action").equals("login")) {
				if ((request.getParameter("login").equals("")) || (request.getParameter("password").equals(""))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("emptyFields"));
	        		return;
				}
				User user = new User(request.getParameter("login"));
				if (user.login(request.getParameter("password"))) {
	        		user.initChildrenItems();
	        		session.setAttribute("user", user);
	        		response.getWriter().write("headerUser##"+WidgetFactory.getLoginConf(user.getLogin()).toString()
	        									+ "|headerMenu##"+WidgetFactory.getTopMenu(user.getType())		
	        									+ "|contentMenu##"+WidgetFactory.getSideMenu(user.getType())		
	        									+ "|footerMenu##"+WidgetFactory.getBottomMenu(user.getType())
												+ "|contentMain##"+WidgetFactory.getAlbumSet(user.getChildrenItems())
	        		);
	        		return;
				} else {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("loginErr"));
	        		return;
				}
			}
			if (request.getParameter("action").equals("checkLoginPseudo")) {
				if (!ItemFactory.isLoginValid(request.getParameter("login"))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("badLogin"));
					return;
				} else {
					response.getWriter().write("ok|ok");
					return;
				}
			}
			if (request.getParameter("action").equals("disconnect")) {
				((User)session.getAttribute("user")).disconnect();
        		User u = new User("shared");
        		u.initChildrenItems();
        		response.getWriter().write("style##default"
        									+ "|headerUser##"+WidgetFactory.getLoginForm().toString()
        									+ "|headerMenu##"+WidgetFactory.getTopMenu(u.getType())		
        									+ "|contentMenu##"+WidgetFactory.getSideMenu(u.getType())		
        									+ "|footerMenu##"+WidgetFactory.getBottomMenu(u.getType())		
        									+ "|contentMain##"+WidgetFactory.getAlbum(new Album())
        		);
	        	session.setAttribute("user", new User("shared"));
	        	session.setAttribute("album", new Album());
	        	
        		return;
			}				
			if (request.getParameter("action").equals("checkRegisterPseudo")) {
				if (!ItemFactory.isLoginValid(request.getParameter("login"))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("badLogin"));
					return;
				} else if (ItemFactory.isLoginTaken(request.getParameter("login"))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("takenLogin"));
					return;
				} else {
					response.getWriter().write("ok|ok");
					return;
				}
			}
			if (request.getParameter("action").equals("checkRegisterPassword")) {
				if (!ItemFactory.isPasswordValid(request.getParameter("password"))) {
        			response.getWriter().write("nok|" + WidgetFactory.getResources().getString("badPassword"));
					return;
				} else {
					response.getWriter().write("ok|ok");
					return;
				}
			}
			if (request.getParameter("action").equals("checkMail")) {
				if (!ItemFactory.isMailValid(request.getParameter("mailAdresse"))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("badMail"));
					return;
				} else {
					response.getWriter().write("ok|ok");
					return;
				}
			}
			if (request.getParameter("action").equals("register")) {
				if ((request.getParameter("login").equals("")) || (request.getParameter("password").equals(""))) {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("emptyFields"));
	        		return;
				}
				User u = new User(request.getParameter("login"));
				boolean accept = new Boolean(request.getParameter("acceptMail"));
				if (accept && request.getParameter("mailAdresse").equals("")) {
					response.getWriter().write("nok|" + WidgetFactory.getResources().getString("badMail"));
					return;
				}
				if (u.register(request.getParameter("password"), accept, request.getParameter("mailAdresse"))) {
					session.setAttribute("user", u);
	        		response.getWriter().write("headerUser##"+WidgetFactory.getLoginConf(u.getLogin()).toString()
							+ "|headerMenu##"+WidgetFactory.getTopMenu(u.getType())		
							+ "|contentMenu##"+WidgetFactory.getSideMenu(u.getType())		
							+ "|footerMenu##"+WidgetFactory.getBottomMenu(u.getType())		
					);
	        		return;
				} else {
	        		response.getWriter().write("nok|" + WidgetFactory.getResources().getString("recErr"));
	        		return;
				}
			}
			if (request.getParameter("action").equals("imageFocus")) {
				if (null != session.getAttribute("album")) {
					Album al = (Album)session.getAttribute("album");
					if (al.getChildrenItems().size() > Integer.parseInt(request.getParameter("id"))) {
		        		response.getWriter().write(/*"style##" + al.getSkin() + "|*/"focusedImg##"+
						WidgetFactory.getFocused(al.getChildrenItems().elementAt(Integer.parseInt(request.getParameter("id")))));
		        		return;
					}
				}
			}
			if (request.getParameter("action").equals("ajaxAlbumDetail")) {
				if (null != session.getAttribute("user")) {
					User u = (User)session.getAttribute("user");
					u.initChildrenItems();
					if (null != request.getParameter("id")) {
						if (u.getChildrenItems().size() > Integer.parseInt(request.getParameter("id"))) {
							((Album)u.getChildrenItems().elementAt(Integer.parseInt(request.getParameter("id")))).initChildrenItems();
							session.setAttribute("album", u.getChildrenItems().elementAt(Integer.parseInt(request.getParameter("id"))));
							response.getWriter().write("contentMain##"+WidgetFactory.getAlbum(u.getChildrenItems().elementAt(Integer.parseInt(request.getParameter("id")))));
						}
					}
					
				}

			}
			
				 
		}
	}
}
